<?php
/**
 * Created by PhpStorm.
 * User: yinnannan
 * Date: 16/2/1
 * Time: 下午10:08
 */

require('application/classes/dao/Utils.php');
class productpageset    {
    public function getAll(){
        $result = DB::select('*')
            ->from('sysproductpageset')->execute()->as_array();
        return $result;
    }


    public function getLoginImg($parm){
        $result = DB::select('*')
            ->from('sysproductpageset')->where("type","=",$parm['type'])->execute()->as_array();
        return $result;
    }

    public function updateLoginImg($parm){
        

        $parm['pic'] = str_replace("\"", "", $parm['pic']);
         moveUploadFile($parm['pic']);
        DB::update("sysproductpageset")->set(array('img' => $parm['pic']))
            ->where('type', '=', 22)->execute();

    }
    public function getByList($parm){
        $result = DB::select('*,(select title from sysproduct where id=sysproductpageset.productId) as title')
            ->from('sysproductpageset')->where("type","=",$parm['type'])->execute()->as_array();
        return $result;
    }
    public function updateBatch($parm){
            self::delete($parm);
            if($parm['type']==1){
                foreach($parm['mainpic'] as $key=>$value){

                    $value = str_replace("\"", "", $value);
                    Utils:: moveUploadFile($value);
                    DB::insert("sysproductpageset",array('productId','img','sort','content','type'))
                        ->values(array($parm['productId'][$key],$value,$parm['sort'][$key],"",$parm['type']))
                        ->execute();
                }
            }
            if($parm['type']==2){
                foreach($parm['mainpic'] as $key=>$value){

                    $value = str_replace("\"", "", $value);
                     Utils::moveUploadFile($value);
                    DB::insert("sysproductpageset",array('productId','img','sort','content','type','title'))
                        ->values(array($parm['productId'][$key],$value,$parm['sort'][$key],$parm['content'][$key],$parm['type'],$parm['title'][$key]))
                        ->execute();
                }
            }
            if($parm['type']==3||$parm['type']==6||$parm['type']==7||$parm['type']==8||$parm['type']==9){
                foreach($parm['mainpic'] as $key=>$value){

                    $value = str_replace("\"", "", $value);
                    Utils:: moveUploadFile($value);
                    DB::insert("sysproductpageset",array('productId','img','sort','type'))
                        ->values(array($parm['productId'][$key],$value,$parm['sort'][$key],$parm['type']))
                        ->execute();
                }
            }

            
    }
    public function updateByType($parm){
        Cache::instance('file')->delete('pageproduct');
        try{
            Database::instance()->begin();
            self::updateBatch($parm);
            Database::instance()->commit();
        } catch (Exception $e) {
            echo $e->getMessage();
            Database::instance()->rollback();
            throw $e;
        }
    }
    public function setByTypeAndHead($parm){
        Cache::instance('file')->delete('pageproduct');
         try{
            Database::instance()->begin();
            self::updateBatch($parm);
            $head['type']=$parm['typeother'];
            self::delete($head);
            //插入头部的设置产品数据
            $parm['pic'] = str_replace("\"", "", $parm['pic']);
             Utils::moveUploadFile($parm['pic']);
            DB::insert("sysproductpageset",array('productId','img','sort','content','type'))
                        ->values(array($parm['fristProduct'][0],$parm['pic'],0,implode(',',$parm['label']),$parm['typeother']))
                        ->execute();
            Database::instance()->commit();
        } catch (Exception $e) {
            echo $e->getMessage();
            Database::instance()->rollback();
            throw $e;
        }
    }


    public function delete($parm){
        Cache::instance('file')->delete_all();
        
        DB::delete('sysproductpageset')->where('type','=',$parm['type'])->execute();
    }
}